package com.smx.sync.util;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * DatabaseConnectionChecker
 *
 * @author 廖功旺
 * @version 1.0
 * @since 2025/6/23 17:15
 */
public class DatabaseConnectionChecker {

    public static boolean checkConnection(String driver, String url, String username, String password) {
        try {
            Class.forName(driver);
            try (Connection connection = DriverManager.getConnection(url, username, password)) {
                return connection != null && !connection.isClosed();
            }
        } catch (ClassNotFoundException | SQLException e) {
            return false;
        }
    }

    public static boolean isDatabaseConnected(DataSource dataSource) {
        try (Connection connection = dataSource.getConnection()) {
            return connection != null && !connection.isClosed();
        } catch (SQLException e) {
            return false;
        }
    }
}
